﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;

public partial class admin_users : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ListItem perfil =null;
        ArrayList perfiles = null;
        if (!Page.IsPostBack)
        {
            string userid = "";
            perfiles = DB.getPerfiles();
            foreach (RE_GenericBean rgb in perfiles) {
                perfil = new ListItem(rgb.strC1, rgb.intC1.ToString());
                chk_perfilList.Items.Add(perfil);
            }


            if (Request.QueryString["userid"] != null && !Request.QueryString["userid"].Trim().Equals(""))
            {
                userid = Request.QueryString["userid"].ToString();
            }

            // como no es vacio significa que tengo que halar los datos
            if (userid != null && !userid.Equals(""))
            {
                RE_GenericBean user = DB.getUserData(userid);
                lb_userid.Text = user.strC1.ToUpper();
                tb_userid.Text = user.strC1.ToUpper();
                tb_password.Text = user.strC2;
                lb_contrasena.Text = user.strC2;
                tb_firstname.Text = user.strC3.ToUpper();
                tb_lastname.Text = user.strC4.ToUpper();
                tb_shortname.Text = user.strC5.ToUpper();
                tb_userid.Enabled = false;
                if (user.arr1 != null && user.arr1.Count > 0)
                {
                    foreach (string perID in user.arr1)
                    {
                        for (int x = 0; x < chk_perfilList.Items.Count; x++)
                        {
                            perfil = (ListItem)chk_perfilList.Items[x];
                            if (perfil.Value.Equals(perID))
                            {
                                chk_perfilList.Items[x].Selected = true;
                            }
                        }
                    }
                }
            }
            
            DataSet ds=DB.getUserDataDS();
            gvd.DataSource = ds.Tables["users"];
            gvd.DataBind();
        }
    }
    protected void gvd_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string userid = gvd.Rows[index].Cells[2].Text;
            Response.Redirect("users.aspx?userid=" + userid);
        }
        if (e.CommandName == "Delete")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string userid = gvd.Rows[index].Cells[2].Text;
            int result = DB.deleteUserData(userid);
        }
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        RE_GenericBean userbean = new RE_GenericBean();
        userbean.strC1=tb_userid.Text.ToUpper();
        if (tb_password.Text.Trim().Equals("")) 
            userbean.strC2 = lb_contrasena.Text;
        else
            userbean.strC2=tb_password.Text;

        userbean.strC3=tb_firstname.Text.ToUpper();
        userbean.strC4=tb_lastname.Text.ToUpper();
        userbean.strC5=tb_shortname.Text.ToUpper();
        for (int a=0; a<chk_perfilList.Items.Count; a++){
            if (chk_perfilList.Items[a].Selected == true) {
                if (userbean.arr1 == null) userbean.arr1 = new ArrayList();
                userbean.arr1.Add(chk_perfilList.Items[a].Value);
            }
        }

        int result = DB.InsertUpdateUserData(lb_userid.Text.ToUpper(), userbean);

    }
    protected void gvd_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[3].Style.Add("display", "none");
        } else if (e.Row.RowType == DataControlRowType.Header) {
            e.Row.Cells[3].Style.Add("display", "none");
        }
    }
    protected void btn_add_goal_Click(object sender, EventArgs e)
    {
        String csname1 = "PopupScript";
        Type cstype = this.GetType();
        ClientScriptManager cs2 = Page.ClientScript;
        string script = "window.open('modifyGoal.aspx?userid=" + tb_userid.Text + "','Goal','menubar=1,resizable=1,scrollbars=1,toolbar=1,width=340');";
        if (!cs2.IsStartupScriptRegistered(cstype, csname1))
        {
            cs2.RegisterStartupScript(cstype, csname1, script, true);
        }

    }
}
